package com.hrt.web.orderInfor.controller;

<<<<<<< HEAD
=======
import java.io.IOException;
>>>>>>> refs/remotes/origin/master
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hrt.base.BaseController;
import com.hrt.bean.GiftDetail;
import com.hrt.bean.OrderDetail;
import com.hrt.bean.OrderInfor;
import com.hrt.bean.OrderStatus;
import com.hrt.bean.PayType;
import com.hrt.vo.OrderDetailVO;
import com.hrt.vo.OrderInforVO;
<<<<<<< HEAD
import com.hrt.web.comments.service.CommentsWebService;
=======
>>>>>>> refs/remotes/origin/master
import com.hrt.web.member.service.HRTMemberService;
import com.hrt.web.orderInfor.service.WebOrderDetailService;
import com.hrt.web.orderInfor.service.WebOrderService;
import com.hrt.web.product.service.GiftDetailService;
@Controller
@RequestMapping("web/orderInfor")
public class WebOrderController extends BaseController<OrderInfor>{
	@Autowired
	private WebOrderService webOrderService;
	@Autowired 
	private  GiftDetailService giftDetailService;
	
	@Autowired
	private WebOrderDetailService webOrderDetailService;
	@Autowired
	private HRTMemberService hrtMemberService;
	
	//评论
	@Autowired
	private CommentsWebService commentsWebService;

	private List<Map<String, Object>> orderStatusList;
	private List<Map<String, Object>> typeList;
	
	public List<Map<String, Object>> getTypeList() {
		typeList = PayType.getTypeList();
		return typeList;
	}

	public void setTypeList(List<Map<String, Object>> typeList) {
		this.typeList = typeList;
	}
	public List<Map<String, Object>> getOrderStatusList() {
		orderStatusList = OrderStatus.getOrderStatusList();
		return orderStatusList;
	}

	public void setOrderStatusList(List<Map<String, Object>> orderStatusList) {
		this.orderStatusList = orderStatusList;
	}
	/**
	 * 
	 * 
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/list")
	public ModelAndView list(@RequestParam(value = "memberId",required = true) Long memberId,
			@RequestParam(value = "status",required = false) Integer status,
			HttpServletRequest request) {
		ModelAndView view = new ModelAndView();
		if(status==-1){
			
			String hql = "select order from OrderInfor order where order.memberId='"+memberId+"' and order.isdelete=0 and order.isView=0";
			list = webOrderService.queryByPage(hql,getPageIndex(), getPageSize());
		}
		else if(status==-2){
			String hql = "select order from OrderInfor order where order.memberId='"+memberId+"' and order.isdelete=0 and order.isView=1";
			list = webOrderService.queryByPage(hql,getPageIndex(), getPageSize());
		}
		else if(status==-3){
			String hql = "select order from OrderInfor order where order.memberId='"+memberId+"' and order.isdelete=0 and order.isView=0 and order.isComment=0 and order.status=4";
			list = webOrderService.queryByPage(hql,getPageIndex(), getPageSize());
		}
		else{
			String hql = "select order from OrderInfor order where order.memberId='"+memberId+"' and order.isdelete=0 and order.isView=0 and order.status='"+status+"'";
			list = webOrderService.queryByPage(hql,getPageIndex(), getPageSize());
			
		}
		this.setTotalCount(webOrderService.getOrderInforByMemberId(memberId,status));
		this.setPageCount(this.computePageCount(this.getTotalCount(),this.getPageSize()));

		page = this.getPage();
		this.orderStatusList = getOrderStatusList();
		this.typeList = getTypeList();
		view.addObject("orderStatusList", orderStatusList);
		view.addObject("typeList", typeList);
		view.addObject("STATUS", status);
		view.addObject("pageResult", page);
		view.setViewName("/HRTWEB/myOrder");
		return view;

	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/queryByPage")
	@ResponseBody
	public Map queryByPage(
			@RequestParam(value = "pageIndex", required = false) Integer pageIndex,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "memberId",required = true) Long memberId,
			@RequestParam(value = "status",required = false) Integer status,
			HttpServletRequest request) {
		Map resultMap = new HashMap();
		if (pageSize == null || "".equals(pageSize)) {
			pageSize = 30;
		}
		if (pageIndex == null || "".equals(pageIndex)) {
			pageIndex = 1;
		}
		setPageIndex(pageIndex);

		setPageSize(pageSize);
		List<OrderInfor> resultList = null;
		if(status==-1){
			String hql = "select order from OrderInfor order where order.memberId='"+memberId+"' and order.isdelete=0 and order.isView=0";
			resultList = webOrderService.queryByPage(hql,getPageIndex(), getPageSize());
		}
		else if(status==-2){
			String hql = "select order from OrderInfor order where order.memberId='"+memberId+"' and order.isdelete=0 and order.isView=1";
			resultList = webOrderService.queryByPage(hql,getPageIndex(), getPageSize());
		}
		else if(status==-3){
			String hql = "select order from OrderInfor order where order.memberId='"+memberId+"' and order.isdelete=0 and order.isView=0 and order.isComment=0 and order.status=4";
			resultList = webOrderService.queryByPage(hql,getPageIndex(), getPageSize());
		}
		else{
			System.out.println("status----------"+status);
			String hql = "select order from OrderInfor order where order.memberId='"+memberId+"' and order.isdelete=0 and order.isView=0 and order.status='"+status+"'";
			resultList = webOrderService.queryByPage(hql,getPageIndex(), getPageSize());
		}
		
		this.setTotalCount(webOrderService.getOrderInforByMemberId(memberId,status));
		setList(resultList);
		for(OrderInfor order:resultList){
			if(order!=null){
				List<OrderDetail> details = webOrderDetailService.getOrderDetailByOrderId(order.getId());
				order.setOrderDetail(details);
				//礼品规格
				for(OrderDetail oDetail:details){
					if(oDetail != null){
						oDetail.setGiftDescr(oDetail.getGiftDetail().getGiftInfor().getDescription());
<<<<<<< HEAD
=======
						System.out.println("sdffffcccccfffsdfsdf==>"+oDetail.getGiftDetail().getGiftInfor().getDescription());
>>>>>>> refs/remotes/origin/master
					}
				}
			}
		}
		
		this.setPageCount(this.computePageCount(this.getTotalCount(),this.getPageSize()));

		page = this.getPage();

		resultMap.put("pageResult", page);
		return resultMap;
	}
	@RequestMapping(value = "/queryByOrderId")
	public ModelAndView queryByOrderId(@RequestParam(value = "orderId",required = true) Long orderId,
			HttpServletRequest request) {
		ModelAndView view = new ModelAndView();
		List<OrderDetail> details = webOrderDetailService.getOrderDetailByOrderId(orderId);
		view.addObject("detailList", details);
		view.setViewName("/HRTWEB/myOrderDetail");
		return view;

	}
	
	/**
	 * 跳到评论页面之前
	 * */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "/commentByOrderId")
	public ModelAndView commentByOrderId(@RequestParam(value = "orderId",required = true) Long orderId,HttpServletRequest request) {
		ModelAndView view = new ModelAndView();
		List<GiftDetail> giftDetails = new ArrayList<GiftDetail>();
		List<OrderDetail> orderDetails = webOrderDetailService.getOrderDetailByOrderId(orderId);
		//用来拼接礼品规格id的字符串
		String gdeIds="";
		//用来判断订单有多少订单详情的数量
		int num = orderDetails.size();
		for(int i= 0; i<orderDetails.size(); i++){
			OrderDetail orderDetail = orderDetails.get(i);
			GiftDetail giftDetail = giftDetailService.getById(orderDetail.getGiftDetailId());
			giftDetail.setOrderDetailId(orderDetail.getId());
			gdeIds = gdeIds+orderDetail.getGiftDetailId()+",";
			giftDetails.add(giftDetail);
		}
		if(gdeIds != null){
			gdeIds=gdeIds.substring(0,gdeIds.length()-1);
		}

		//全部评论
		
		List  resultList= new ArrayList();
		if(gdeIds==null || gdeIds==""){
			throw new RuntimeException("礼品规格id有误");
		}else{
			List<Long> gdeIdsList = commentsWebService.findCommIdByGdeId(gdeIds);
			
			String gdeIdstr = "";
			for(Long sonId : gdeIdsList ){
				gdeIdstr = gdeIdstr+sonId+",";
			}
			if(gdeIdstr != ""){
				gdeIdstr=gdeIdstr.substring(0,gdeIdstr.length()-1);
			}
			resultList = commentsWebService.queryById(gdeIdstr, this.getPageIndex(),
					this.getPageSize());
		
			setTotalCount(commentsWebService.findNumByGdeId(gdeIdstr));
		}
		this.setList(resultList);
		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));
		page = this.getPage();
		view.addObject("gdeIds", gdeIds);
		view.addObject("pageResult", page);
		
		view.addObject("commGiftDetails", giftDetails);
		view.addObject("num", num);
		view.setViewName("/HRTWEB/commentPage/commentPage");
		return view;
		
	}
	/**
	 * 保存订单
	 * @param orderInforVO
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public ModelAndView save(@ModelAttribute("webOrderInfo") OrderInforVO orderInforVO,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView();
		System.out.println("save");
		System.out.println("orderInforVO----------"+orderInforVO.getSummery());
		System.out.println("orderInforVO:" + orderInforVO.getPostCode());
		orderInforVO.setIsdelete(0);
		orderInforVO.setStatus(0);
		orderInforVO.setOrderSource(1);
		orderInforVO.setSender("何人堂");
		orderInforVO.setCreateTime(this.webOrderService.getDate());
		List<OrderDetailVO> details = new ArrayList<OrderDetailVO>();  
			
				
				for(OrderDetailVO od:orderInforVO.getDetails()){
					od.setIsdelete(0);
					od.setCreateTime(this.webOrderService.getDate()
							);
					
					/*od.setCreator(this.getOnlineManager(request, response)
							.getLoginName());
					od.setEditor((this.getOnlineManager(request, response)
							.getLoginName()));*/
					od.setEditTime(this.webOrderService.getDate());
					details.add(od);
				}
				webOrderService.saveOrderInfor(orderInforVO, details);
			
			view.setViewName("/HRTWEB/order_success");
			return view;
	}
	/**
	 * 保存订单
	 * @param orderInforVO
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public ModelAndView save(@ModelAttribute("webOrderInfo") OrderInforVO orderInforVO,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView();
		System.out.println("save");
		System.out.println("orderInforVO----------"+orderInforVO.getSummery());
		System.out.println("orderInforVO:" + orderInforVO.getPostCode());
		orderInforVO.setIsdelete(0);
		orderInforVO.setStatus(0);
		orderInforVO.setOrderSource(1);
		orderInforVO.setSender("何人堂");
		orderInforVO.setCreateTime(this.webOrderService.getDate());
		List<OrderDetailVO> details = new ArrayList<OrderDetailVO>();  
			
				
				for(OrderDetailVO od:orderInforVO.getDetails()){
					od.setIsdelete(0);
					od.setCreateTime(this.webOrderService.getDate()
							);
					
					/*od.setCreator(this.getOnlineManager(request, response)
							.getLoginName());
					od.setEditor((this.getOnlineManager(request, response)
							.getLoginName()));*/
					od.setEditTime(this.webOrderService.getDate());
					details.add(od);
				}
				webOrderService.saveOrderInfor(orderInforVO, details);
			
			view.setViewName("/HRTWEB/order_success");
			return view;
	}
	/**
	 * 保存订单
	 * @param orderInfor
	 * @param giftDetailId
	 * @param request
	 * @param response
	 * @return
	 *//*
	@RequestMapping(value = "/save",method = RequestMethod.POST)
	public ModelAndView save(@ModelAttribute("webOrderInfo") OrderInfor orderInfor,
			@RequestParam(value = "giftDetailId", required = true) Long giftDetailId,
			HttpServletRequest request,HttpServletResponse response){
			ModelAndView view = new ModelAndView();
			HRTMember member = hrtMemberService.getMemberByid(orderInfor.getMemberId());
			//订单基本信息
			orderInfor.setHrtMember(member);
			orderInfor.setOrderSource(0);
			orderInfor.setSender("何人堂");
			orderInfor.setCreateTime(this.webOrderService.getDate());
			//订单详细信息
			OrderDetail orderDetail = new OrderDetail();
			orderDetail.setCreateTime(this.webOrderDetailService.getDate());
			orderDetail.setCreator(member.getMbrName());
			orderDetail.setEditor(member.getMbrName());
			orderDetail.setEditTime(this.webOrderDetailService.getDate());
			
			GiftDetail giftDetail = giftDetailService.getGiftDetailById(giftDetailId);
			orderDetail.setGiftDetail(giftDetail);
		try {
			webOrderService.saveOrder(orderInfor);
			orderDetail.setOrderInfor(orderInfor);
			webOrderDetailService.saveOrderDetail(orderDetail);
			
		} catch (Exception 	e) {
			e.printStackTrace();
		}
		view.setViewName("/HRTWEB/order_success");
		return view;
		
	}*/
	/**
	 * 删除订单
	 * @param orderId
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public String delete(@RequestParam(value = "orderId",required = true) Long orderId,
			HttpServletRequest request,HttpServletResponse response) {
		OrderInfor orderInfor = webOrderService.getOrderByid(orderId);
		orderInfor.setIsView(new Integer(1));
		webOrderService.updateOrder(orderInfor);
		return "success";
	}
	/**
	 * 恢复订单
	 * @param orderId
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/recovery")
	@ResponseBody
	public String recovery(@RequestParam(value = "orderId",required = true) Long orderId,
			HttpServletRequest request,HttpServletResponse response) {
		OrderInfor orderInfor = webOrderService.getOrderByid(orderId);
		orderInfor.setIsView(new Integer(0));
		webOrderService.updateOrder(orderInfor);
		return "success";
	}
	/**
	 * 彻底删除订单
	 * @param orderId
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/destory")
	@ResponseBody
	public String destory(@RequestParam(value = "orderId",required = true) Long orderId,
			HttpServletRequest request,HttpServletResponse response) {
		OrderInfor orderInfor = webOrderService.getOrderByid(orderId);
		orderInfor.setIsView(new Integer(2));
		webOrderService.updateOrder(orderInfor);
		return "success";
	}
}
